<?php

namespace App\Admin\Actions\ShopOrderReturn;

use App\Library\Logic\OpenIdLogic;
use App\Library\Logic\OperateLogLogic;
use App\Library\Logic\ShopOrderLogic;
use App\Library\Service\Payment\WechatService;
use App\Library\Service\WxMessageService;
use App\Models\ShopOrderReturnModel;
use Encore\Admin\Actions\Response;
use Encore\Admin\Facades\Admin;
use Exception;

class ShopOrderReturnAllOnlyAction extends ShopOrderReturnAuction
{
    public $name = '全额退款';//仅退款

    /**
     * handle
     * @param ShopOrderReturnModel $model
     * @return Response
     * @throws Exception
     */
    public function handle(ShopOrderReturnModel $model): Response
    {
        if ((int)$model->or_type !== 2) return $this->response()->error('参数错误')->refresh();
        $this->saveModel($model);
        $model->save();
        payLog(WechatService::refund($model)->toArray());
        OperateLogLogic::add(Admin::user()->user_id, 'admin.orderReturn.money');
        self::salesHandle($model, '已同意');
        return $this->response()->success('操作成功')->refresh();
    }

    /**
     * handle
     * @param ShopOrderReturnModel $model
     * @return void
     */
    public function form(ShopOrderReturnModel $model): void
    {
        $ensurePrice = $model->or_ensure_price * 1;
        $str = "本次退款金额为￥{$model->or_real_price}，确定要给用户全额退款吗？订单保证金：￥{$ensurePrice}（发货后保证金不退）";
        $this->text('or_real_price', '退款金额')->default($model->or_real_price)->readonly();
        $this->text('or_ensure_price', '保费')->default($ensurePrice)->readonly()->help($str);
    }
}
